<?php
namespace submodule\modules\forms\components;

use Yii;
use yii\db\Schema;
use yii\db\Migration;

class Uninstaller extends Migration
{
	public $controller;
	
	public $action = 'install';
	
	public function __construct($controller){
		$this->controller = $controller;
		
	}
    //安装
    public function install($models, $field_tablename)
    {
		$moduleid = $models[0]->modelid;
		$modelid = isset($models[1]) ? $models[1]->modelid : false;
		$tablename = $models[0]->tablename;
		//建表
        Yii::$app->db->createCommand()->createTable('{{%'.$tablename.'}}', [
            'formid'=>  "pk",
			'title'=>  "varchar(80) NOT NULL default ''",
			'status'=>  "tinyint(2) unsigned NOT NULL default '1'",
			'addtime'=>  "int(10) unsigned NOT NULL default '0'",
			'content'=>  "text",
			'userid'=>  "int(10) unsigned NOT NULL default '0'",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();


		
		//记录字段
		$fields = ['modelid', 'name', 'field', 'formtype', 'fieldtype', 'fieldlength', 'listorder', 'disabled', 'primarytable', 'required', 'setting', 'param', 'api', 'allowinput'];

		
		$rows = [
		     [$modelid, 'ID', 'formid', 'primarykey', 'int', 10, 0, 0, 1, 1, '', '', '', 0], 
			 [$modelid, '标题', 'title', 'text', 'varchar', 80, 0, 0, 1, 1, '', '', '', 1], 
			 [$modelid, '状态', 'status', 'switch', 'tinyint', 1, 0, 0, 1, 1, '', '', '', 1], 
			 [$modelid, '发布时间', 'addtime', 'text', 'int', 10, 0, 0, 1, 1, '', '', '', 0], 
			 [$modelid, '内容', 'content', 'ueditor', '', 150, 0, 0, 1, 0, '', '', '', 1],
			 [$modelid, '用户', 'userid', 'text', 'int', 10, 0, 0, 1, 0, '', '', '', 0],
			 
			 
			 
	    ];
		if ($this->action == 'install'){
			//模块配置项字段
			 array_push($rows, [$moduleid, '登录提交', 'islogin', 'switch', 'int', 10, 0, 0, 2, 1, '', '', '', 0]);
		}
		
		Yii::$app->db->createCommand()->batchInsert($field_tablename, $fields, $rows)->execute();
		return [$modelid, $moduleid];
		
    }
	
	/**
	* 新建
	*
	*/
	public function create($model, $field_tablename){
		$this->action = 'create';
		self::install($model, $field_tablename);
	}
    
	
	//卸载
    public function uninstall()
    {
        $this->dropTable('{{store_product}}');
    }
}
